﻿using System;
using Business;
using System.Text;
using System.Data;
using Utilities;

namespace TimeWork.webmaster.manager
{
    public partial class UserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["mail"] == null)
            {
                Response.Redirect("http://" + Request.Url.Authority + "/webmaster/SignIn.aspx");
            }
            if (!IsPostBack)
            {
                loadCompany();
                loadContent();
            }
        }

        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["companyid"] = ddlCompany.SelectedValue;
            loadContent();
        }

        private void loadCompany()
        {
            ddlCompany.DataSource = BuCompany.selAllCompany();
            ddlCompany.DataTextField = "CompanyName";
            ddlCompany.DataValueField = "CompanyID";
            ddlCompany.DataBind();
            if (Session["companyid"] == null)
            {
                Session["companyid"] = "1";
            }

            ddlCompany.SelectedValue = Session["companyid"].ToString();
        }

        private void loadContent()
        {
            ltrData.Text = getListUser(Session["companyid"].ToString()).ToString();
        }

        private StringBuilder getListUser(string companyID)
        {
            StringBuilder result = new StringBuilder();
            result.Append(@"<table cellspacing='0'><thead><tr>
			               <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:29px;border: 1px solid #CCC;'>STT</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:250px;border: 1px solid #CCC;'>Email</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:60px;border: 1px solid #CCC;'>Họ tên</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #CCC;'>Phòng ban</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #CCC;'>Chức vụ</th>
                           <th style='text-align:center;color:#375B91;background-color:#F3F9FF;width:80px;border: 1px solid #CCC;'>Quyền hạn</th>
                           </tr></thead><tbody>");
            DataTable data = BuStaff.selAllStaff(companyID);

            int numOfRow = data.Rows.Count;
            DataRow dataRow;
            for (int i = 0; i < numOfRow; i++)
            {
                dataRow = data.Rows[i];
                result.Append("<tr><td style='text-align:center;border: 1px solid #CCC;'>" + (i + 1) + "</td>");
                // Email
                result.Append("<td style='border: 1px solid #CCC;'><a href='UserModified.aspx?cid="+UtiGeneralClass.EncodeStringToBase64(companyID)+"&");
                result.Append("email=" + UtiGeneralClass.EncodeStringToBase64(dataRow["StaffEmail"].ToString()) + "'>");
                result.Append("<strong>" + dataRow["StaffEmail"].ToString() + "</strong></a></td>");
                // Name
                result.Append("<td style='border: 1px solid #CCC;'>" + dataRow["StaffName"].ToString() + "</td>");
                // ResultUnitName
                result.Append("<td style='border: 1px solid #CCC;'>" + dataRow["ResultUnitName"].ToString() + "</td>");
                // StaffPosition
                result.Append("<td style='border: 1px solid #CCC;'>" + dataRow["StaffPosition"].ToString() + "</td>");
                // PermissionName
                result.Append("<td style='border: 1px solid #CCC;'>" + dataRow["PermissionName"].ToString() + "</td></tr>");
            }
            result.Append("</tbody></table>");
            return result;
        }
    }
}